package com.healthcloud.service.impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.healthcloud.dao.DoctorAndPatientsRelationMapper;
import com.healthcloud.dao.HospDrAssoMapper;
import com.healthcloud.dao.PatientMapper;
import com.healthcloud.dao.PhysicalExamMapper;
import com.healthcloud.entity.Condition;
import com.healthcloud.entity.DoctorAndPatientsRelation;
import com.healthcloud.entity.HospDrAsso;
import com.healthcloud.entity.Patient;
import com.healthcloud.entity.PatientCriteria;
import com.healthcloud.entity.PhysicalExam;
import com.healthcloud.query.CriteriaUtils;
import com.healthcloud.service.DoctorAndPatientsRelationService;
import com.healthcloud.utils.DateUtils;
import com.healthcloud.utils.Pagine;
import com.healthcloud.vo.PatientVo;


/**
 *  医生-病人-医院 关联关系 接口实现
 * @author ZhengL
 * 2015-08-20
 */
@Service
@Transactional
public class DoctorAndPatientsRelationServiceImpl implements
		DoctorAndPatientsRelationService {
	
	@Autowired
	private DoctorAndPatientsRelationMapper dprMapper;
	@Autowired
	private HospDrAssoMapper hdaMapper;
	@Autowired
	private PhysicalExamMapper physicalExamMapper;
	@Autowired
	private PatientMapper pMapper;
	
	@Override
	public int save(DoctorAndPatientsRelation entity) {
		return dprMapper.save(entity);
	}

	@Override
	public int update(DoctorAndPatientsRelation entity) {
		return dprMapper.update(entity);
	}

	@Override
	public int delete(Long id) {
		return dprMapper.delete(id);
	}
	@Override
	public DoctorAndPatientsRelation create(String hosId,String docId,String idCard){
		DoctorAndPatientsRelation entity2 = new DoctorAndPatientsRelation();
		if(StringUtils.isNotBlank(docId)){
			HospDrAsso entity1 = new HospDrAsso();
			entity1.setDrId(Long.valueOf(docId));
			if(StringUtils.isBlank(hosId)){
				List<HospDrAsso> list1 = hdaMapper.findByEntityAll(entity1);
				if(list1!=null&&list1.size()>0){
					entity2.setHospitalId(list1.get(0).getHospId());
				}
			}
			entity2.setDoctorId(Long.valueOf(docId));
		}
		if(StringUtils.isNotBlank(hosId)){
			entity2.setHospitalId(Long.valueOf(hosId));
		}
		if(StringUtils.isNotBlank(idCard)){
			Patient pt = pMapper.findByIdCard(idCard);
			if(pt!=null){
				entity2.setPatientId(pt.getId());
			}
		}
		List<DoctorAndPatientsRelation> list2 = dprMapper.findByEntity(entity2, null);
		if(list2==null||list2.size()==0){
			dprMapper.save(entity2);
			return entity2;
		}else{
			return list2.get(0);
		}
	}
	
	@Override
	public List<PatientVo> findPatientsByRelation(String hosId,String docId,String range){
		List<PatientVo> result = new ArrayList<PatientVo>();
		DoctorAndPatientsRelation entity = new DoctorAndPatientsRelation();
		if(StringUtils.isNotBlank(docId)){
			HospDrAsso entity1 = new HospDrAsso();
			entity1.setDrId(Long.valueOf(docId));
			List<HospDrAsso> list1 = hdaMapper.findByEntityAll(entity1);
			if(list1!=null&&list1.size()>0){
				HospDrAsso hda = list1.get(0);
				if(StringUtils.isNotBlank(hosId)){
					entity.setHospitalId(Long.valueOf(hosId));
				}else{
					entity.setHospitalId(hda.getHospId());
				}
			}
			List<PatientVo> patients = dprMapper.findPatients(entity,null);
			result = setResult(range,docId,patients);//遍历筛选结果集
		}
		return result;
	}
	
	//筛选病人列表
	private List<PatientVo> setResult(String range,String docId,List<PatientVo> patients){
		List<PatientVo> result = new ArrayList<PatientVo>();
		Map<String,String> tempMap = new HashMap();//暂存病人编号
		if(patients!=null&&patients.size()>0){
			if(range=="0"||range.equals("0")){//筛选与医生关联的病人
				for(PatientVo vo : patients){
					String patientId = vo.getPatientId();
					String doctorId = vo.getDoctorId();
					if(tempMap.containsKey(patientId)){//避免重复的病人id
						continue;
					}else{
						if(StringUtils.isNotBlank(doctorId)){
							if(vo.getDoctorId()==docId||vo.getDoctorId().equals(docId)){
								result.add(setExamDate(vo));
								tempMap.put(patientId, vo.getIdCard());
							}
						}
					}
				}
			}else if(range=="1"||range.equals("1")){//筛选与医院关联的病人
				for(PatientVo vo : patients){
					String patientId = vo.getPatientId();
					if(tempMap.containsKey(patientId)){//避免重复的病人id
						continue;
					}else{
						result.add(setExamDate(vo));
						tempMap.put(patientId, vo.getIdCard());
					}
				}
			}else{
				
			}
		}
		return result;
	}
	
	@Override
	public Page<Patient> findPatientsByCriteria(PatientCriteria criteria, Pageable pageable) {
		List<Condition<Object>> conditions = CriteriaUtils.getConditions(criteria);
		Pagine<Patient> pagine = new Pagine<>(pageable);
		dprMapper.findPatientsByCriteria(conditions, pagine);
		return pagine;
	}
	
	//关联最后一次体检记录
	private PatientVo setExamDate(PatientVo vo){
		String sqlWhere = " 1=1";
		String idCard = vo.getIdCard();
		sqlWhere += " physicalexamination.idcard=" + idCard;
		sqlWhere += " GROUP BY idcard ORDER BY examDate";
		if (StringUtils.isBlank(vo.getDoctorId())) {
			List<PhysicalExam> li = physicalExamMapper.customCondition(sqlWhere);
			if (li != null && li.size() > 0) {
				PhysicalExam pe = li.get(0);
				Date dt = pe.getExamDate();
				if (dt != null) {
					String dStr = DateUtils.format(DateUtils.Date2Calendar(dt),"yyyy-MM-dd HH:mm:ss");
					vo.setLastExamDate(dStr);
				}
			}
		}
		return vo;
	}
}
